package com.test.design.material.materialdesign;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * author:harisucici on 2018/8/1 14:37
 * email:harisucici@gmail.com
 * name:
 * todo:
 */
public class TabActivity extends AppCompatActivity {
    List<Fragment> fragments = new ArrayList<>(2);
//    private String [] titles = {"one5555555555555","two","three","four"};
    List<String> titles = new ArrayList<>(2);
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);
        final ViewPager viewpager = findViewById(R.id.viewpager);

        TabLayout tablayout = findViewById(R.id.tablayout);

        tablayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tablayout.setTabMode(TabLayout.MODE_SCROLLABLE);

//        FragmentTest fragment1 = new FragmentTest();
//        FragmentTest fragment2 = new FragmentTest();
//        FragmentTest fragment3 = new FragmentTest();
//        FragmentTest fragment4 = new FragmentTest();
//
//        fragments.add(fragment1);
//        fragments.add(fragment2);
//        fragments.add(fragment3);
//        fragments.add(fragment4);

        for(int i = 0;i<8;i++){
            fragments.add(new FragmentTest());
            titles.add("6666666666");
        }


        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
        viewpager.setAdapter(adapter);
        tablayout.setupWithViewPager(viewpager);
        for (int i = 0; i < adapter.getCount(); i++) {
            TabLayout.Tab tab = tablayout.getTabAt(i);//获得每一个tab
            tab.setCustomView(R.layout.item_tab);//给每一个tab设置view
            if (i == 0) {
                // 设置第一个tab的TextView是被选择的样式
                tab.getCustomView().findViewById(R.id.tab_text).setSelected(true);//第一个tab被选中
            }

            TextView textView = tab.getCustomView().findViewById(R.id.tab_text);
            textView.setText(titles.get(i));//设置tab上的文字
        }
        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.tab_text).setSelected(true);
                viewpager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.tab_text).setSelected(false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private class FragmentAdapter extends FragmentPagerAdapter {
        private List<Fragment> fragmentList;
        public FragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }
        @Override
        public int getCount() {
            return fragmentList.size();
        }
//        @Override
//        public CharSequence getPageTitle(int position) {
//            return title[position];
//        }
    }
}
